package com.syf.kit.soa.core.spi.nacos;

import cn.hutool.core.util.StrUtil;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.syf.kit.soa.config.SOARegionClientConfiguration;
import com.syf.kit.soa.core.SOARegionInfo;
import com.syf.kit.soa.core.spi.SOARegionStorage;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class NacosSOAStorage implements SOARegionStorage {
    private NacosClient client;

    private SOARegionInfo soaRegionInfo;

    public NacosSOAStorage() {
    }

    @Override
    public void loadConfig(SOARegionClientConfiguration config) {
        log.info("服务注册时，nacos连接配置信息：{}", config);
        if (StrUtil.isBlank(config.getUsername())) {
            this.client = new NacosClient(config.getConnUrl());
        } else {
            this.client = new NacosClient(config.getConnUrl(), config.getUsername(), config.getPassword());
        }
    }

    @Override
    public void setServiceRegionInfo(SOARegionInfo regionInfo) {
        this.soaRegionInfo = regionInfo;
    }

    @Override
    public void registerServiceRegion() {
        try {
            client.getNamingService().registerInstance(soaRegionInfo.getRegionName(), soaRegionInfo.getRunEnv(), soaRegionInfo.getHost(), soaRegionInfo.getListenPort());
            log.info("向nacos，完成service region[{}]的服务自动注册，注册信息：[{}]", soaRegionInfo.getRegionName(), soaRegionInfo);
        } catch (NacosException e) {
            log.error("nacos注册出现异常，nacos client配置信息：{}", client);
            throw new RuntimeException(e);
        }
    }

    @Override
    public void deregisterServiceRegion() {
        try {
            Instance instance = new Instance();
            instance.setIp(soaRegionInfo.getHost());
            instance.setPort(soaRegionInfo.getListenPort());
            instance.setClusterName(soaRegionInfo.getRunEnv());
            Integer weight = new Integer(soaRegionInfo.getWeight());
            instance.setWeight(weight.doubleValue());
            client.getNamingService().deregisterInstance(soaRegionInfo.getRegionName(), soaRegionInfo.getHost(), soaRegionInfo.getListenPort(), soaRegionInfo.getRunEnv());
            log.info("向nacos注销service region:[{}]成功", instance);
        } catch (NacosException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<SOARegionInfo> discoveryService(String regionName) {
        List<SOARegionInfo> soaRegionInfos = new ArrayList<>();
        try {
            List<Instance> allInstances = client.getNamingService().getAllInstances(regionName, soaRegionInfo.getRunEnv());
            for (Instance instance : allInstances) {
                SOARegionInfo regionInfo = new SOARegionInfo();
                regionInfo.setRegionName(regionName);
                regionInfo.setHost(instance.getIp());
                regionInfo.setListenPort(instance.getPort());
                regionInfo.setRunEnv(instance.getClusterName());
                Double weight = new Double(instance.getWeight());
                regionInfo.setWeight(weight.intValue());
                soaRegionInfos.add(regionInfo);
            }
        } catch (NacosException e) {
            throw new RuntimeException(e);
        }
        return soaRegionInfos;
    }
}
